﻿


var GetCount = function(){
        timeFuture = new Date();
        timeFuture = timeFuture.getTime() - (timeFuture.getHours()*60*60*1000) - (timeFuture.getMinutes() * 60 * 1000) - (timeFuture.getSeconds() * 1000) + 41400000 - timeFuture.getTimezoneOffset() *60*100;
        dateNow = new Date(); //grab current date
        dateNow = dateNow.getTime() -  dateNow.getTimezoneOffset() *60*100;
        if(dateNow > timeFuture){
            timeFuture += 86400000;
        }
        amount = timeFuture - dateNow; //calc milliseconds between dat
        delete dateNow;

		days=0;hours=0;mins=0;secs=0;out="";

		amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs

		days=Math.floor(amount/86400);//days
		amount=amount%86400;

		hours=Math.floor(amount/3600);//hours
		amount=amount%3600;

		mins=Math.floor(amount/60);//minutes
		amount=amount%60;

		secs=Math.floor(amount);//seconds

		if(hours<10) out += "0"
		if (days != 0 || hours != 0) { out += hours + ":"; }
		if (mins < 10) out += "0"
		if (days != 0 || hours != 0 || mins != 0) { out += mins + ":"; }
		if (secs < 10) out += "0"
		out += secs;
		
		return out;
        
};

        var countDownEntry = function(){
            Ext.getCmp('countdownTimer').update("<div style='margin-top:10px;text-align:center'><div style='font-size:16px'>Lunch Countdown</div><div style='font-size:40px'>"+GetCount()+"</div></div>"); 
                this.countDown = new Ext.util.DelayedTask(function () {
                this.countDownEntry();
            }, this);
            this.countDown.delay(1000);
    };


    app.views.Vote = Ext.extend(Ext.Panel, {
        dockedItems: [{
            xtype: 'toolbar',
            title: 'Vote'
        }],
        layout: {
            type: 'vbox',
            pack: 'center'
        },
        items: [{
            xtype: 'panel',
            id: 'countdownTimer',
            html: "<div style='margin-top:10px;text-align:center'><div style='font-size:16px'>Lunch Countdown</div><div style='font-size:40px'>"+GetCount()+"</div></div>"
        }, {
            xtype: 'form',
            items: [{
                xtype: 'fieldset',
                defaults: {
                    labelAlign: 'left'
                },
                items: [{
                    xtype: 'togglefield',
                    id: 'voteToggle',
                    label: 'Are you going to lunch?',
                    listeners: {
                        'change': function (field, field2, newval, oldval) {

                            if (oldval != newval) {
                                Ext.Ajax.request({
                                    url: 'http://lunchpad.me/service/Service1.svc/sendoptin?userId=1&lat2=47.612644&long2=-122.199211&vote=1' 
       
//                                    success: function (response, opts) {
//                                        alert('success');
//                                    },
//                                    failure: function (response, opts) { alert(response.status); }
                            });
                        }
                    }
                }
            }]
        }]
    }, {
        xtype: 'button',
        width: '175px',
        id: 'viewAttending',
        text: 'View Attendees',
        handler: function () {
            Ext.dispatch({
                controller: app.controllers.navigation,
                action: 'attendeeShow'
            });
        }
    }],

    initComponent: function () {
        app.views.Vote.superclass.initComponent.call(this);


        countDownEntry();
    }

});





var sleep = function(milli){
    stop = new Date().getTime() + milli;
    while( new Date().getTime() <= stop);
};
    //Ext.getCmp('countdownTimer').update('html', "'<h1>'+GetCount()+'</h1>'"); 